<?php

namespace App\Admin\Controllers;

use App\Models\IotCard;
use App\Models\Cabinet;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use App\Models\ProjectUser;
use App\Models\Project;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Handlers\IotCardHandler;

class IotCardController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('物联卡列表')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(new IotCard(), function (Grid $grid) {
            $grid->column('box_name', '名称');
            $grid->column('iccid', 'iccid');

            $grid->column('active_time', '激活时间')->display(function () {
                return IotCardHandler::cardInfoCache($this->imsi, 'active_time');
            });
            $grid->column('status_name', '状态')->display(function () {
                return IotCardHandler::cardInfoCache($this->imsi, 'status_name');
            });
            $grid->column('used_flow', '使用量(KB)')->display(function () {
                return IotCardHandler::cardInfoCache($this->imsi, 'used_flow');
            });
            $grid->column('day_flow', '最近日流量(KB)')->display(function () {
                return IotCardHandler::cardInfoCache($this->imsi, 'day_flow');
            });
            $grid->column('total_month_flow', '当月总流量(KB)')->display(function () {
                return IotCardHandler::cardInfoCache($this->imsi, 'total_month_flow');
            });
            $grid->column('validity_date', '到期日期')->display(function () {
                return IotCardHandler::cardInfoCache($this->imsi, 'validity_date');
            });

            $grid->disableViewButton();
        });
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建电源')
            ->description('创建')
            ->body($this->form());
    }

    public function form(){
        return Form::make(new IotCard(), function (Form $form) {
            $form->text('iccid', 'iccid')->required();
            
            $projectList = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
            $boxes = Cabinet::where('pid','0')->whereIn('project_id',$projectList)->pluck('name','id')->toArray();
            $form->select('box_id', '关联设备')->options($boxes)->required();
            $form->hidden('box_name');
            $form->hidden('imsi');

            $form->saving(function (Form $form) use ($boxes) {
                $cardInfo = IotCardHandler::cardInfo($form->iccid);
                if (!$cardInfo) {
                    return $form->response()->error('无效的iccid');
                } else {
                    $form->box_name = $boxes[$form->box_id];
                    $form->imsi = $cardInfo['imsi'];
                }
            });
            $form->disableViewButton();
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('物联卡编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }
}